package com.pine.app.module.security.core.config;

import com.pine.app.module.security.core.authorize.decisions.*;
import com.pine.app.module.security.core.authorize.filter.SecurityAuthorizeFilter;
import com.pine.app.module.security.core.authorize.manager.MethodAuthorizeDataManager;
import com.pine.app.module.security.core.authorize.manager.impl.DefaultMethodAuthorizeDataManagerImpl;
import com.pine.app.module.security.core.authorize.metadataSource.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

/**
 * @author xiaoyuan
 * @create 2020/3/13 17:37
 **/
@Slf4j
public class WebMethodSecurityConfigurerAdapter {


    private AuthorizeProperties authorizeProperties;

    public WebMethodSecurityConfigurerAdapter(
            AuthorizeProperties authorizeProperties
    ) {

        this.authorizeProperties = authorizeProperties;

    }


//    @Bean
//    public MethodAuthorizeAspect methodAuthorizeAspect(){
//        return new MethodAuthorizeAspect();
//    }
//
//    @Bean
//    public MethodAuthorizeInterceptor methodAuthorizeInterceptor(){
//        return new MethodAuthorizeInterceptor();
//    }
//
//
//
//
//    @Bean
//    public ResourceAuthorizeManager resourceAuthorizeManager(){
//        return new DefaultResourceAuthorizeManagerImpl();
//    }
    @Bean
    @ConditionalOnMissingBean(MethodAuthorizeDataManager.class)
    public MethodAuthorizeDataManager methodAuthorizeDataManager(){
        return new DefaultMethodAuthorizeDataManagerImpl();
    }


    @Bean
    public SecurityMetadataSource securityMetadataSource() {
        return new DefaultSecurityMetadataSource();
    }

    @Bean
    public FilterInvocationSecurityMetadataSource filterInvocationSecurityMetadataSource() {
        return new FilterInvocationSecurityMetadataSource();
    }

    @Bean
    @ConditionalOnMissingBean(SecurityLoadMethodDataSource.class)
    public SecurityLoadMethodDataSource securityLoadMethodDataSource() {
        return new YmlSecurityLoadMethodDataSource(authorizeProperties);
    }

    @Bean
    public SecurityAuthorizeFilter securityAuthorizeFilter() {
        return new SecurityAuthorizeFilter();
    }


    @Bean
    @ConditionalOnMissingBean(AnonAuthorizeDecision.class)
    public AnonAuthorizeDecision anonAuthorizeDecision(){
        return new AnonAuthorizeDecision();
    }

    @Bean
    @ConditionalOnMissingBean(PortAuthorizeDecision.class)
    public PortAuthorizeDecision portAuthorizeDecision(){
        return new PortAuthorizeDecision();
    }
    @Bean
    @ConditionalOnMissingBean(PermsAuthorizeDecision.class)
    public PermsAuthorizeDecision permsAuthorizeDecision(){
        return new PermsAuthorizeDecision();
    }
    @Bean
    @ConditionalOnMissingBean(RoleAuthorizeDecision.class)
    public RoleAuthorizeDecision roleAuthorizeDecision(){
        return new RoleAuthorizeDecision();
    }
    @Bean
    @ConditionalOnMissingBean(ScopeAuthorizeDecision.class)
    public ScopeAuthorizeDecision scopeAuthorizeDecision(){
        return new ScopeAuthorizeDecision();
    }
}
